<?php
/*
 * Created on 22.12.2005
 *
 * action => action to call for remote data
 *
 */
 
class ajax_comboBoxHandler
{
    
    var $tagName = 'input'; 
    var $containerTag = false; 

    function startElement (& $sender, $attrs)
    {
        $form = $sender->topParent();
        if ($form['type'] != 'form')
        {
            die('ajax:comboBox must not be used within other tag than h:form or ajax:form (flow control tags)!');
        }
        if ($attrs['validate'] && (!$form['name'] || !$attrs['name']))
        {
            die("If ajax:comboBox tag has attribute validate than it also needs attribute name and the surrounding form tag must have attribute name too!");
        }
        if (!$attrs['dataUrl'])
        {
            die("ajax:comboBox tag must have dataUrl attribute!");
        }
        // tag code
        $code = "\$out->write('<".$this->tagName;
        $attrs['dojoType'] = "'combobox'";
        $attrs['mode'] = "'remote'";
        foreach ($attrs as $n => $v)
        {
            if ($n != 'pspc' && $n != 'validate' && $n != 'validationParam' 
                && $n != 'validationErrorClass' && $n != 'validationErrorStyle'
                && $n != 'class' && $n != 'style' && $n != 'checked')
            {
                $code .= " $n=\"'.$v.'\"";
            }
        }
        $code .= " formInputName=\"'.".$attrs['name'].".'\"";
        $code .= "');\n";
        if ($attrs['checked'])
            $code .= 'if ('.$attrs['checked'].') $out->write(" checked=\"checked\"");'."\n";
        $code .= "\$out->write(phpinchains_tagsupport_HtmlInput::getValidationErrorCode(\$actionform, ".(isset($attrs['validate'])?$attrs['validate']:"''").", ".
            (isset($form['name'])?$form['name']:"''").", ".(isset($attrs['name'])?$attrs['name']:"''").", ".(isset($attrs['class'])?$attrs['class']:"''").", ".(isset($attrs['style'])?$attrs['style']:"''").", ".
            (isset($attrs['validationErrorClass'])?$attrs['validationErrorClass']:"''").", ".(isset($attrs['validationErrorStyle'])?$attrs['validationErrorStyle']:"''")."));\n";
        if ($this->containerTag)
            $code .= "\$out->write('>');\n";
        else
            $code .= "\$out->write('/>');\n";
        
        $code .= "\$out->write('<script type=\"text/javascript\">dojo.require(\"dojo.widget.ComboBox\");dojo.addOnLoad(function () {dojo.widget.byId(\"'.".$attrs['id'].".'\").setValue(\"'.".$attrs['value'].".'\")});</script>');\n";
                        
        $sender->appendCode($code);
        // validation code
        $code = '';
        if ($attrs['validate'])
        {
            $code .= "\n        \$this->validateField(\$env, \$actionform, ".$attrs['name'].", ".(isset($attrs['validationParam'])?$attrs['validationParam']:"''").', '.$attrs['validate'].");\n";
            $sender->appendValidationCode($code);
        }
    }

    function endElement (& $sender)
    {
    }

}

?>